cc.Class({
    extends: cc.Component,

    properties: {
        pfUser:cc.Prefab,        
        btnJoin:cc.Button,
        btnJinZhi:cc.Button,
        nodeCard:cc.Node,
        nodeHand:cc.Node,
        spHead:cc.Sprite,
        startTouchX:null,
        nodeUserContent:cc.Node,
        showUserNum:0,
        passNumerArr:[],
        nodeItem:null,
        txtLevel:cc.Label,
        txtCondition:cc.Label,
        isTouch:true,
    },

    onLoad () {
        window.initGameConfigData();
        this.txtLevel.string = "第" + window.gameConfig.gameData.level + "关"
        cc.director.preloadScene("gameEnd");
        this.createPfUser(window.gameConfig.gameData.level);
    },

    start () {
        this.node.on(cc.Node.EventType.TOUCH_START,this.onStartTouch,this);
        this.node.on(cc.Node.EventType.TOUCH_END,this.onEndTouch,this);
    },

    //开始触摸
    onStartTouch:function(event){
        if(!this.isTouch)return;
        //记录开始触摸的世界坐标
        this.startTouchX = event.getLocationX();
    },

    //结束触摸
    onEndTouch:function(event){
        if(!this.isTouch)return;
        let endTouchX = event.getLocationX();
        //判断玩家滑动方向
        if(this.startTouchX > endTouchX)
        {
            this.onButtonJinZhiCheck();
        }
        else if(this.startTouchX < endTouchX)
        {
            this.onButtonJoinCheck();
        }
    },

    //允许进入
    onButtonJoinCheck:function(){
        cc.log("允许进入");
        this.showUserCard("headJoinAnim",true);
    },

    //禁止进入
    onButtonJinZhiCheck:function(){
        cc.log("禁止进入");
        this.showUserCard("headJinZhiAnim",false);
    },

    showUserCard:function(name, type){
        if(this.nodeItem.isPass == type)this.passNumerArr.push(true);
        else 
        {
            //判断错误  调用手机震动
            if('vibrate' in window.navigator) {
                window.navigator.vibrate(300); //震动
            }else {
                console.log("浏览器不支持震动");
            }

            this.passNumerArr.push(false);
        }

        //禁用进入和禁止按钮
        var self = this;
        this.btnJoin.interactable = false;
        this.btnJinZhi.interactable = false;
        this.isTouch = false;//禁止触摸

        var nodeUserArr = this.nodeUserContent.children;
        nodeUserArr[nodeUserArr.length - this.showUserNum].active = false;
        if(nodeUserArr.length - this.showUserNum > 0)
        {
            //播放游戏玩家入场动画
            let anim = this.nodeHand.getComponent(cc.Animation);
            anim.play();

            this.scheduleOnce(()=>{
                //开启按钮
                self.btnJoin.interactable = true;
                self.btnJinZhi.interactable = true;
                self.isTouch = true;
            }, 2);

            //设置卡片信息
            this.showUserNum += 1;
            var showUser = nodeUserArr[nodeUserArr.length - this.showUserNum];
            var userInfo = showUser.getComponent("pfUser");
            this.nodeItem = userInfo;
            showUser.active = true;
            this.setCardDataInfo(userInfo.age,userInfo.tiWen);
        }
        else
        {
            //跳转场景
            cc.director.loadScene("gameEnd");
            window.passNum = this.passNumerArr;
            this.nodeHand.active = false;
            cc.log(this.passNumerArr)
        }
    },

    setCardDataInfo:function(age,tiwen){
        var cardInfo = this.nodeCard.getComponent("cardContent");
        cardInfo.setCardContentData(age,tiwen);
    },

    //生成游戏玩家
    createPfUser:function(number){
        var levelData = window.getGameLevelDataConfig(number);
        this.txtCondition.string = levelData.tiWenCondition;
        var memberArr = levelData.member.split(",")

        for(let i = memberArr.length - 1; i >= 0; i--)
        {
            let nodeUser = window.createPrefab(this.pfUser);
            let userItem = nodeUser.getComponent("pfUser");
            userItem.setUserData(memberArr[i], levelData.passCondition, levelData.tiWenCondition, levelData.gameScene);
            this.nodeUserContent.addChild(nodeUser);
            if(i == 0)
            {
                this.nodeItem = userItem
                nodeUser.active = true;
                this.setCardDataInfo(userItem.age,userItem.tiWen);
                this.showUserNum += 1;
            }
        }
    },
});
